USE [APEX_TESTING]
GO
/****** Object:  StoredProcedure [dbo].[sp_AB_AgingReportOne]    Script Date: 11/17/2013 22:21:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_AB_AgingReportOne]
as
--exec [sp_AB_AgingReportOne]
create table TempDueDateOne  (tempDueDateOne datetime)
insert into TempDueDateOne exec AB_GetDocDueDateOne

select
--------------------Company Details---------------------------------------
T1.CompnyName,T1.Phone1, T1.Fax, T1.FreeZoneNo, T1.TaxIdNum,
T1.CompnyAddr, T1.Block, T1.Street, T1.Country,T1.ZipCode,T1.LogoImage,
--------------------Outgoing Payment Details------------------------------ 
T0.DocEntry as DocNo, T0.CardCode, T0.CardName, T0.DocDate as PostingDate, T0.DocDueDate as DueDate,
isnull(T0.PrjCode,0)as ProjectCode,T0.OpenBal as BalanceDue
from OVPM T0 with(nolock)
---------------------------------------------------------------------------
join 
(
 select top(1) isnull(T0.PrintHeadr,T0.CompnyName) CompnyName,T0.Phone1, T0.Fax, T0.FreeZoneNo, T0.TaxIdNum,T1.ZipCode,
  T0.CompnyAddr,T1.Block,T1.Street,T3.LogoImage,
  T2.Name Country
 from OADM T0 with(nolock) 
 join ADM1 T1 with(nolock) on 1=1
 join OCRY T2 with(nolock) on T2.Code=T1.Country
 join OADP T3 with(nolock) on 1=1
) T1 on 1=1

where [docduedate] IN (SELECT * from  TempDueDateOne)
and OpenBal>0 and DocType='C'
order by T0.DocDueDate, T0.CardCode


drop table TempDueDateOne



